//
//  VRMeViewController.m
//  VRFirefighting
//
//  Created by Veer on 2018/8/9.
//  Copyright © 2018年 Veer. All rights reserved.
//

#import "VRMeViewController.h"

#import "JKSettingViewController.h"
#import "VRMessageViewController.h"
#import "VRMyInfoViewController.h"
#import "VRFirefightingDeviceViewController.h"
#import "VRFirefightingRecordViewController.h"
#import "VRApplyTestViewController.h"
#import "VRRiskReportViewController.h"
#import "VRBindDeviceTypeViewController.h"
#import "VRUserGroupViewController.h"
#import "VRMyAdressViewController.h"

@interface VRMeViewController ()

@property (nonatomic, strong) UIScrollView *contentScrollView1;
@property (nonatomic, strong) UIImageView *headerView;

@property (nonatomic, strong) UILabel *nameLbl;
@property (nonatomic, copy) NSString *mobile;

@end

@implementation VRMeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = ScrollViewBgColor;
    self.title = @"我的";
    
    [self setupNav];
    [self setupSubView];
    [self getMyInfo];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccess) name:kLoginSuccess object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logout) name:kLogout object:nil];
}

- (void)setupNav
{
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.tag = 0;
    [leftBtn setImage:[UIImage imageNamed:@"icon_about"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(navBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    leftBtn.frame = CGRectMake(0, 0, 44, 44);
    leftBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    leftBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.tag = 1;
    [rightBtn setImage:[UIImage imageNamed:@"fg_mine_message_pic"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(navBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    rightBtn.frame = CGRectMake(0, 0, 44, 44);
    rightBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    rightBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}

- (void)setupSubView
{
    self.headerView.y = 0;
    
    NSArray *imgNameArray = @[@"my_address", @"my_info", @"my_xfzz", @"my_fire1", @"my_test", @"my_report", @"binding_device", @"user_group"];
    NSArray *titleArray = @[@"我的地址", @"我的信息", @"消防设备", @"消防安全记录", @"设备测试", @"隐患报告", @"绑定设备", @"用户组"];
    
    CGFloat itemW = kScreenWidth * 0.5;
    CGFloat itemH = 92;
    UIView *lastView = self.headerView;
    for (int i = 0; i < imgNameArray.count; i++) {
        
        CGFloat itemX = i % 2 * itemW;
        CGFloat itemY = CGRectGetMaxY(self.headerView.frame) + i / 2 * itemH;
        UIButton *itemBtn = [self createitemWithImageName:imgNameArray[i] title:titleArray[i] size:CGSizeMake(itemW, itemH)];
        itemBtn.tag = i;
        itemBtn.x = itemX;
        itemBtn.y = itemY;
        [itemBtn addTarget:self action:@selector(itemBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentScrollView1 addSubview:itemBtn];
        
        if (i % 2 == 1) {
            
            UIView *vLineView = [[UIView alloc] init];
            vLineView.backgroundColor = RGB(140, 140, 140);
            vLineView.frame = CGRectMake(itemBtn.x - 0.5, itemBtn.y, 0.5, itemH);
            [self.contentScrollView1 addSubview:vLineView];
        }
        
        UIView *hLineView = [[UIView alloc] init];
        hLineView.backgroundColor =  RGB(140, 140, 140);
        hLineView.frame = CGRectMake(itemBtn.x, CGRectGetMaxY(itemBtn.frame) - 1, itemW, 0.5);
        [self.contentScrollView1 addSubview:hLineView];
        lastView = hLineView;
    }
    
    self.contentScrollView1.contentSize = CGSizeMake(kScreenWidth, CGRectGetMaxY(lastView.frame));
}

- (UIButton *)createitemWithImageName:(NSString *)imageName title:(NSString *)title size:(CGSize)size
{
    UIButton *itemBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    itemBtn.backgroundColor = [UIColor whiteColor];
    itemBtn.size = size;
    
    UIImageView *headerIconImgView = [[UIImageView alloc] init];
    headerIconImgView.contentMode = UIViewContentModeScaleAspectFit;
    headerIconImgView.image = [UIImage imageNamed:imageName];
    [itemBtn addSubview:headerIconImgView];
    [headerIconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(itemBtn);
        make.centerY.mas_equalTo(itemBtn).offset(-12);
        make.width.height.mas_equalTo(32);
    }];
    
    UILabel *titleLbl = [[UILabel alloc] init];
    titleLbl.text = title;
    titleLbl.textColor = MAINColor;
    titleLbl.font = VRFont(16);
    titleLbl.textAlignment = NSTextAlignmentCenter;
    [itemBtn addSubview:titleLbl];
    [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(headerIconImgView);
        make.top.mas_equalTo(headerIconImgView.mas_bottom).offset(6);
    }];
    
    return itemBtn;
}


#pragma mark - networking
- (void)getMyInfo
{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"UserID"] = [VRUser shareUser].token;
    
    weakify(self)
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [JKNetworking postWithSign:GetMyInfo parameters:param success:^(NSDictionary *responseObject) {
        
        strongify(self)
        NSLog(@"responseObject:%@", responseObject);
        NSInteger code = [responseObject[@"state"] integerValue];
        NSString *msg = responseObject[@"message"];
        
        if (code == 0) {
            
            self.mobile = responseObject[@"data"][@"fPhone"];
            dispatch_async(dispatch_get_main_queue(), ^{
                
                self.nameLbl.text = responseObject[@"data"][@"fName"];
                [hud hideAnimated:YES];
            });
        }else if(msg.length > 0){
            dispatch_async(dispatch_get_main_queue(), ^{
                [SVProgressHUD showErrorWithStatus:msg];
                [hud hideAnimated:YES];
            });
        }
    } error:^(NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showErrorWithStatus:@"未知错误"];
            [hud hideAnimated:YES];
        });
    }];
}


#pragma mark - action
- (void)navBtnClick:(UIButton *)sender
{
    if (sender.tag == 0) {
        
        JKSettingViewController *setttingVc = [[JKSettingViewController alloc] init];
        [self.navigationController pushViewController:setttingVc animated:YES];
    }else if (sender.tag == 1){
        
        VRMessageViewController *setttingVc = [[VRMessageViewController alloc] init];
        [self.navigationController pushViewController:setttingVc animated:YES];
    }
}

- (void)itemBtnClick:(UIButton *)sender
{
    NSArray<NSString *> *classArray = @[@"VRMyAdressViewController", @"VRMyInfoViewController", @"VRFirefightingDeviceViewController", @"VRFirefightingRecordViewController", @"VRApplyTestViewController", @"VRRiskReportViewController", @"VRBindDeviceTypeViewController", @"VRUserGroupViewController"];
    
    if (sender.tag < classArray.count && classArray[sender.tag].length > 0) {
        
        Class typeName = NSClassFromString(classArray[sender.tag]);
        UIViewController *curVc = [[typeName alloc] init];
        if (sender.tag == 4) {
            ((VRApplyTestViewController *)curVc).nameStr = self.nameLbl.text;
            ((VRApplyTestViewController *)curVc).mobile = self.mobile;
        }else if (sender.tag == 7){
            ((VRUserGroupViewController *)curVc).nameStr = self.nameLbl.text;
            ((VRUserGroupViewController *)curVc).mobile = self.mobile;
        }
        [self.navigationController pushViewController:curVc animated:YES];
    }
}

- (void)loginSuccess
{
    
    [self getMyInfo];
}

- (void)logout
{
    
}


#pragma mark - lazy
- (UIScrollView *)contentScrollView1
{
    if (_contentScrollView1 == nil) {
        
        _contentScrollView1 = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        _contentScrollView1.backgroundColor = ScrollViewBgColor;
//        _contentScrollView1.delegate = self;
        //        _contentScrollView.bounces = NO;
        _contentScrollView1.alwaysBounceVertical = YES;
        //        _contentScrollView.delegate = self;
        if (@available(iOS 11.0, *)) {
//            _contentScrollView1.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        [self.view addSubview:_contentScrollView1];
    }
    return _contentScrollView1;
}

- (UIImageView *)headerView
{
    if (_headerView == nil) {
        
        _headerView = [[UIImageView alloc] init];
        _headerView.contentMode = UIViewContentModeScaleAspectFill;
        _headerView.image = [UIImage imageNamed:@"mine_top"];
        _headerView.frame = CGRectMake(0, 0, kScreenWidth, 136);
        [self.contentScrollView1 addSubview:_headerView];
        
        UIImageView *headerIconImgView = [[UIImageView alloc] init];
        headerIconImgView.contentMode = UIViewContentModeScaleAspectFill;
        headerIconImgView.image = [UIImage imageNamed:@"icon_logo"];
        [_headerView addSubview:headerIconImgView];
        [headerIconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self->_headerView);
            make.centerY.mas_equalTo(self->_headerView).offset(-16);
            make.width.height.mas_equalTo(74);
        }];
        
        UILabel *titleLbl = [[UILabel alloc] init];
        titleLbl.text = @"";
        titleLbl.textColor = RGB(255, 255, 255);
        titleLbl.font = VRFont(16);
        titleLbl.textAlignment = NSTextAlignmentCenter;
        [_headerView addSubview:titleLbl];
        self.nameLbl = titleLbl;
        [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self->_headerView);
            make.top.mas_equalTo(headerIconImgView.mas_bottom).offset(10);
        }];
        
    }
    return _headerView;
}

@end
